'''
Tasks that can be performed by Wolfit processes.

@author: Philip van Oosten
@license: GNU GPLv2
'''
from wolfit import BaseTask

def register_task(task_class):
    BaseTask.registry.append(task_class._register)
    return task_class

@register_task
class CallableTask(BaseTask):
    '''
    A process created by wrapping a callable. Keyword arguments can be used as
    input variables. The return value should be a dict-like object, which maps
    strings to output The same callable can be used many times.
    '''

    def __init__(self, process, implementation, name = None):
        BaseTask.__init__(self, process)
        self._implementation = implementation
        if name:
            self.name = name + ' (' + implementation.__name__ + ')'
        else:
            self.name = implementation.__name__

    def _perform(self, input):
        '''
        @param input: A dictionary-like object mapping names to values.
        '''
        output = self._implementation(**input) if input else self._implementation()
        return output
    
    @classmethod
    def _register(cls, process, implementation):
        task = None
        if callable(implementation):
            task = CallableTask(process, implementation)
        return task


###class _ConnectionTask(BaseTask):
###    '''
###    A placeholder task to ease import from or export to data formats containing explicit
###    connection modules, like the data from the Wolfit Editor (django-wolfit).
###    This task can not be executed, but should be discarded in a second pass of
###    the importer, or generated in a first pass of an exporter.
###    '''
###    def __init__(self, process):
###        super(_ConnectionTask, self).__init__(process)
###        self.output_to_input_map = []
###    
###    def add_mapping(self, output_name, input_name):
###        self.output_to_input_map.append((output_name, input_name))
###    
###    def add_mappings(self, mappings):
###        for output_name, input_name in mappings:
###            self.add_mapping(output_name, input_name)
###    
